home *** CD-ROM | disk | FTP | other *** search
/ PC Users 1998 June / Cd Pc Users 9.iso / prog / inst / baslibs / basstrin.bas < prev    next >
Encoding:
BASIC Source File  |  1996-12-18  |  6.6 KB  |  292 lines

  1. Attribute VB_Name = "basString"
  2. Option Explicit
  3.  
  4. Private Declare Function IsCharAlpha Lib "user32" _
  5.    Alias "IsCharAlphaA" (ByVal cChar As Byte) As Long
  6.    
  7. Private Declare Function IsCharAlphaNumeric Lib _
  8.    "user32" Alias "IsCharAlphaNumericA" (ByVal cChar As Byte) As Long
  9.  
  10.  
  11. '
  12. ' This function returns the Nth token in a string
  13. '    Ex.  GetWord("This is a test.", " ", 2) = "is"
  14. '
  15. Public Function GetToken(s As String, token As String, ByVal Nth As Integer) As String
  16.    Dim i As Integer
  17.    Dim p As Integer
  18.    Dim r As Integer
  19.  
  20.    If Nth < 1 Then
  21.       GetToken = ""
  22.       Exit Function
  23.    End If
  24.  
  25.    r = 0
  26.  
  27.    For i = 1 To Nth
  28.       p = r
  29.       r = InStr(p + 1, s, token)
  30.       If r = 0 Then
  31.          If i = Nth Then
  32.             GetToken = Mid$(s, p + 1, Len(s) - p)
  33.          Else
  34.             GetToken = ""
  35.          End If
  36.          Exit Function
  37.       End If
  38.    Next i
  39.  
  40.    GetToken = Mid$(s, p + 1, r - p - 1)
  41. End Function
  42. '
  43. '  Returns an array to tokenized values
  44. '  Ex:  GetTokens("This is a test.") = ({ "This", "is", "a", "test." })
  45. '
  46. Public Function GetTokens(sTxt As String, sToken As String) As Variant
  47.     Dim iTokenLen As Integer
  48.     Dim iTokenCnt As Integer
  49.     Dim lOffset As Long
  50.     Dim lPrevOffset As Long
  51.     Dim aTokens() As String
  52.  
  53.     iTokenLen = Len(sToken)
  54.     lOffset = InStr(sTxt, sToken)
  55.     
  56.     Do While lOffset > 0
  57.         ReDim Preserve aTokens(iTokenCnt)
  58.         If lOffset - lPrevOffset > 1 Then
  59.             aTokens(iTokenCnt) = Mid$(sTxt, lPrevOffset + 1, lOffset - 1 - lPrevOffset)
  60.         Else
  61.             aTokens(iTokenCnt) = ""
  62.         End If
  63.         
  64.         lPrevOffset = lOffset
  65.         lOffset = InStr(lOffset + iTokenLen, sTxt, sToken)
  66.         iTokenCnt = iTokenCnt + 1
  67.     Loop
  68.     
  69.     ReDim Preserve aTokens(iTokenCnt)
  70.     aTokens(iTokenCnt) = Mid$(sTxt, lPrevOffset + 1)
  71.     GetTokens = CVar(aTokens)
  72. End Function
  73. ' String functions.
  74. ' Converts a double to a string
  75. ' Note:  numbers after the decimal place
  76. '        are ignored.
  77. Function Int2String(ByVal l As Double) As String
  78.    Dim tmp As String
  79.    Dim str As String
  80.    Dim i As Integer
  81.    Dim j As Integer
  82.    
  83.    tmp = Format(l, "000000000000")
  84.    str = ""
  85.    
  86.    ' Opps... it's more than 999 trillion
  87.    ' One could easily add bigger number
  88.    ' support.
  89.    If Len(tmp) > 12 Then
  90.       Int2String = ""
  91.       Exit Function
  92.    End If
  93.    
  94.    
  95.    ' zero is a special case.
  96.    ' you may want to change this to "no"
  97.    ' as in "no dollars and 12/100" for writing
  98.    ' checks.
  99.    
  100.    If Val(tmp) = 0 Then
  101.       Int2String = "zero"
  102.       Exit Function
  103.    End If
  104.    
  105.    
  106.    i = Val(Left$(tmp, 3))
  107.    If i <> 0 Then
  108.       GoSub do_hundreds
  109.       str = str + " trillion"
  110.    End If
  111.    
  112.    i = Val(Mid$(tmp, 4, 3))
  113.    If i <> 0 Then
  114.       GoSub do_hundreds
  115.       str = str + " million"
  116.    End If
  117.    
  118.    i = Val(Mid$(tmp, 7, 3))
  119.    If i <> 0 Then
  120.       GoSub do_hundreds
  121.       str = str + " thousand"
  122.    End If
  123.    
  124.       
  125.    i = Val(Right$(tmp, 3))
  126.    If i <> 0 Then
  127.       GoSub do_hundreds
  128.    End If
  129.    
  130.    Int2String = str
  131.    Exit Function
  132.    
  133.    
  134.  
  135. do_hundreds:
  136.    If i > 99 Then
  137.       j = i
  138.       i = i \ 100
  139.       GoSub do_ones
  140.       str = str + " hundred"
  141.       i = j Mod 100
  142.    End If
  143.  
  144.    If i <> 0 Then
  145.       GoSub do_tens
  146.    End If
  147.    Return
  148.    
  149. do_tens:
  150.    Select Case i Mod 100
  151.       Case 90 To 99:
  152.          str = str + " ninety"
  153.          GoSub do_ones
  154.       Case 80 To 89:
  155.          str = str + " eighty"
  156.          GoSub do_ones
  157.       Case 70 To 79:
  158.          str = str + " seventy"
  159.          GoSub do_ones
  160.       Case 60 To 69:
  161.          str = str + " sixty"
  162.          GoSub do_ones
  163.       Case 50 To 59:
  164.          str = str + " fifty"
  165.          GoSub do_ones
  166.       Case 40 To 49:
  167.          str = str + " fourty"
  168.          GoSub do_ones
  169.       Case 30 To 39:
  170.          str = str + " thirty"
  171.          GoSub do_ones
  172.       Case 20 To 29:
  173.          str = str + " twenty"
  174.          GoSub do_ones
  175.          
  176.       Case 19: str = str + " nineteen"
  177.       Case 18: str = str + " eighteen"
  178.       Case 17: str = str + " seventeen"
  179.       Case 16: str = str + " sixteen"
  180.       Case 15: str = str + " fifteen"
  181.       Case 14: str = str + " fourteen"
  182.       Case 13: str = str + " thirteen"
  183.       Case 12: str = str + " twelve"
  184.       Case 11: str = str + " eleven"
  185.       Case 10: str = str + " ten"
  186.       
  187.       Case Else
  188.          GoSub do_ones
  189.    End Select
  190.    Return
  191.    
  192.    
  193. do_ones:
  194.    If i < 10 Or i Mod 10 = 0 Then
  195.       str = str + " "
  196.    Else
  197.       str = str + "-"
  198.    End If
  199.    
  200.    Select Case i Mod 10
  201.       Case 9: str = str + "nine"
  202.       Case 8: str = str + "eight"
  203.       Case 7: str = str + "seven"
  204.       Case 6: str = str + "six"
  205.       Case 5: str = str + "five"
  206.       Case 4: str = str + "four"
  207.       Case 3: str = str + "three"
  208.       Case 2: str = str + "two"
  209.       Case 1: str = str + "one"
  210.    End Select
  211.    
  212.    Return
  213. End Function
  214.  
  215.  
  216. '
  217. ' Returns 0 if the string is alpha.
  218. ' otherwise returns the position of the first character
  219. ' that failed the test.
  220. '
  221. Public Function IsStringAlpha(s As String) As Long
  222.    Dim i As Long
  223.    
  224.    For i = 1 To Len(s)
  225.       If IsCharAlpha(Asc(Mid$(s, i, 1))) = 0 Then
  226.          IsStringAlpha = i
  227.          Exit Function
  228.       End If
  229.    Next i
  230.    
  231.    IsStringAlpha = 0
  232. End Function
  233.  
  234. '
  235. ' Returns 0 if the string is alphaNumeric
  236. ' otherwise returns the position of the first character
  237. ' that failed the test.
  238. '
  239. Public Function IsStringAlphaNumeric(s As String) As Long
  240.    Dim i As Long
  241.    
  242.    For i = 1 To Len(s)
  243.       If IsCharAlphaNumeric(Asc(Mid$(s, i, 1))) = 0 Then
  244.          IsStringAlphaNumeric = i
  245.          Exit Function
  246.       End If
  247.    Next i
  248.    
  249.    IsStringAlphaNumeric = 0
  250. End Function
  251. '
  252. ' Returns 0 if the string is Numeric
  253. ' otherwise returns the position of the first character
  254. ' that failed the test.
  255. '
  256. Public Function IsStringNumeric(s As String) As Long
  257.    Dim i As Long
  258.    Dim j As Byte
  259.    
  260.    For i = 1 To Len(s)
  261.       j = Asc(Mid$(s, i, 1))
  262.       If IsCharAlphaNumeric(j) = 1 Then
  263.          If IsCharAlpha(j) = 1 Then
  264.             IsStringNumeric = i
  265.             Exit Function
  266.          End If
  267.       Else
  268.          IsStringNumeric = i
  269.          Exit Function
  270.       End If
  271.    Next i
  272.    
  273.    IsStringNumeric = 0
  274. End Function
  275. 'trim a string returned from a system function.
  276. 'ie. kill the 0.
  277. Public Function STrim(s As String) As String
  278.    Dim i As Integer
  279.    Dim s2 As String
  280.    
  281.    s2 = Trim(s)
  282.    i = InStr(s2, Chr$(0))
  283.    
  284.    If i > 0 Then
  285.       s2 = Left$(s2, i - 1)
  286.    End If
  287.    
  288.    STrim = s2
  289. End Function
  290.  
  291.  
  292.